/*
 * Copyright 2011-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.platform.system.role.action;

import java.util.List;
import jakarta.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import me.ijleex.platform.framework.entity.Result;
import me.ijleex.platform.framework.page.Page;
import me.ijleex.platform.framework.spring.ControllerSupport;
import me.ijleex.platform.system.role.entity.Role;
import me.ijleex.platform.system.role.entity.RoleMenu;
import me.ijleex.platform.system.role.service.IRoleMenuService;
import me.ijleex.platform.system.role.service.IRoleService;

/**
 * 管理员角色 信息操作处理
 *
 * @author liym
 * @since 2019-03-14 11:19:03 By 'codegen'
 */
@RequestMapping("/system/role")
@RestController
public class RoleAction extends ControllerSupport<Role> {

    private final IRoleService service;
    private final IRoleMenuService roleMenuService;

    public RoleAction(IRoleService service, IRoleMenuService roleMenuService) {
        this.service = service;
        this.roleMenuService = roleMenuService;
    }

    /**
     * 新增 角色
     */
    @PostMapping("/add")
    public Result add(Role o) {
        int x = this.service.insertRole(o);
        return Result.of(x);
    }

    /**
     * 删除 角色
     */
    @PostMapping("/remove")
    public Result remove(String ids) {
        int x = this.service.deleteRoleByIds(ids);
        return Result.of(x);
    }

    /**
     * 修改 角色
     */
    @PostMapping("/update")
    public Result update(Role o) {
        int x = this.service.updateRole(o);
        return Result.of(x);
    }

    /**
     * 查询 角色列表
     */
    @GetMapping("/list")
    public Page<Role> getRoleList(HttpServletRequest request, Role o) {
        this.setPageParams(request, o);
        List<Role> list = this.service.getRoleList(o);
        return this.wrapPageData(list, o);
    }

    // ===

    /**
     * 得到已授权给指定角色的菜单的 id 列表
     *
     * @param request 请求
     * @param roleId 角色 ID
     * @return 菜单ID 列表
     */
    // http://localhost:8082/ij-platform/system/role/menuIds?roleId=376597004234641409
    @GetMapping("/menuIds")
    public List<Long> getMenuIdByRoleId(HttpServletRequest request, Long roleId) {
        return this.roleMenuService.getMenuIdByRoleId(roleId);
    }

    /**
     * 角色授权
     *
     * <p>建立角色与菜单的关联关系</p>
     *
     * @param request 请求
     * @param object 管理员、角色关系信息，必须包含 roleId、menuIds 等
     * @return 返回信息
     */
    // curl -d "roleId=376597004234641408&menuIds=526550660093693953,526550660093693954" http://localhost:8082/ij-platform/system/role/grant
    @PostMapping("/grant")
    public Result grant(HttpServletRequest request, RoleMenu object) {
        object.setCreator("me");
        return this.roleMenuService.grant(object);
    }

}
